package king.dominic.jlibrary.util;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * Created by king on 2017/5/5.
 * TimeUtils
 */
@SuppressWarnings("unused")
public class TimeUtils {
    private TimeUtils() {
    }

    private static int dateInt(String date) {
        if (TextUtils.isEmpty(date)) return 0;
        if (date.startsWith("0")) {
            date = date.substring(1);
            return dateInt(date);
        }
        return Integer.parseInt(date);
    }

    @SuppressLint("SimpleDateFormat")
    public static int YEAR() {
        return dateInt(new SimpleDateFormat("yyyy").format(System.currentTimeMillis()));
    }

    @SuppressLint("SimpleDateFormat")
    public static int MONTH() {
        return dateInt(new SimpleDateFormat("MM").format(System.currentTimeMillis()));
    }

    @SuppressLint("SimpleDateFormat")
    public static int DAY() {
        return dateInt(new SimpleDateFormat("dd").format(System.currentTimeMillis()));
    }

    @SuppressLint("SimpleDateFormat")
    public static String timestamp2time(long timestamp) {
        return new SimpleDateFormat("yyyy/MM/dd HH:mm").format(timestamp * 1000);
    }

    @SuppressLint("SimpleDateFormat")
    public static String timestamp2time(long timestamp, String format) {
        return new SimpleDateFormat(format).format(timestamp * 1000);
    }

    @SuppressLint("SimpleDateFormat")
    public static String timestamp2timeSHORT(long timestamp) {
        return new SimpleDateFormat("yyyy-MM-dd").format(timestamp * 1000);
    }

    @SuppressLint("SimpleDateFormat")
    public static String timestamp2timeSHORT_CN(long timestamp) {
        return new SimpleDateFormat("yyyy年MM月dd日").format(timestamp * 1000);
    }

    @SuppressLint("SimpleDateFormat")
    public static long time2timestampSHORT(String date) {
        try {
            return new SimpleDateFormat("yyyy-MM-dd").parse(date).getTime() / 1000;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }
    @SuppressLint("SimpleDateFormat")
    public static long time2timestamp(String date,String format) {
        try {
            return new SimpleDateFormat(format).parse(date).getTime() / 1000;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
